<template>  
    <div class="content"></div>  
</template>  

<script>  
 function utf8ByteToUnicodeStr(utf8Bytes){
	    var unicodeStr ="";
	    for (var pos = 0; pos < utf8Bytes.length;){
	      var flag= utf8Bytes[pos];
	      var unicode = 0 ;
	      if ((flag >>>7) === 0 ) {
	        unicodeStr+= String.fromCharCode(utf8Bytes[pos]);
	        pos += 1;
	 
	      } else if ((flag &0xFC) === 0xFC ){
	        unicode = (utf8Bytes[pos] & 0x3) << 30;
	        unicode |= (utf8Bytes[pos+1] & 0x3F) << 24;
	        unicode |= (utf8Bytes[pos+2] & 0x3F) << 18;
	        unicode |= (utf8Bytes[pos+3] & 0x3F) << 12;
	        unicode |= (utf8Bytes[pos+4] & 0x3F) << 6;
	        unicode |= (utf8Bytes[pos+5] & 0x3F);
	        unicodeStr+= String.fromCodePoint(unicode) ;
	        pos += 6;
	 
	      }else if ((flag &0xF8) === 0xF8 ){
	        unicode = (utf8Bytes[pos] & 0x7) << 24;
	        unicode |= (utf8Bytes[pos+1] & 0x3F) << 18;
	        unicode |= (utf8Bytes[pos+2] & 0x3F) << 12;
	        unicode |= (utf8Bytes[pos+3] & 0x3F) << 6;
	        unicode |= (utf8Bytes[pos+4] & 0x3F);
	        unicodeStr+= String.fromCodePoint(unicode) ;
	        pos += 5;
	 
	      } else if ((flag &0xF0) === 0xF0 ){
	        unicode = (utf8Bytes[pos] & 0xF) << 18;
	        unicode |= (utf8Bytes[pos+1] & 0x3F) << 12;
	        unicode |= (utf8Bytes[pos+2] & 0x3F) << 6;
	        unicode |= (utf8Bytes[pos+3] & 0x3F);
	        unicodeStr+= String.fromCodePoint(unicode) ;
	        pos += 4;
	 
	      } else if ((flag &0xE0) === 0xE0 ){
	        unicode = (utf8Bytes[pos] & 0x1F) << 12;;
	        unicode |= (utf8Bytes[pos+1] & 0x3F) << 6;
	        unicode |= (utf8Bytes[pos+2] & 0x3F);
	        unicodeStr+= String.fromCharCode(unicode) ;
	        pos += 3;
	 
	      } else if ((flag &0xC0) === 0xC0 ){ //110
	        unicode = (utf8Bytes[pos] & 0x3F) << 6;
	        unicode |= (utf8Bytes[pos+1] & 0x3F);
	        unicodeStr+= String.fromCharCode(unicode) ;
	        pos += 2;
	 
	      } else{
	        unicodeStr+= String.fromCharCode(utf8Bytes[pos]);
	        pos += 1;
	      }
	    }
	    return unicodeStr;
	  }
var main,receiver,filter;    
var codeQueryTag = false;    
export default {  
    data() {  
        return {  
            scanCode: ''  
        }  
    },  
    created: function (option) {  
        this.initScan()  
        this.startScan();    
    },    
    onHide:function(){    
        this.stopScan();    
    },  
    destroyed:function(){    
        /*页面退出时一定要卸载监听,否则下次进来时会重复，造成扫一次出2个以上的结果*/    
        this.stopScan();    
    },    
    methods: {  
        initScan() {  
            let that = this;  
            main = plus.android.runtimeMainActivity();//获取activity  
            var IntentFilter = plus.android.importClass('android.content.IntentFilter');   
            filter = new IntentFilter();    
            filter.addAction("scan.rcv.message"); // 换你的广播动作  
            receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver',{  
            onReceive : function(context, intent) {
				
                plus.android.importClass(intent);     
				let code = intent.getByteArrayExtra('barocode');
				let codeStr =utf8ByteToUnicodeStr(code);
				console.log('codeStr:', codeStr);
                that.queryCode(codeStr);    
            }});    
        },    
        startScan(){
            main.registerReceiver(receiver,filter);  
        },    
        stopScan(){  
			
            main.unregisterReceiver(receiver);    
        },    
        queryCode: function(codeStr){  
		
            //防重复  
            if(codeQueryTag)return false;    
            codeQueryTag = true;    
            setTimeout(function(){    
                codeQueryTag = false;    
            },150);  
            
            
            uni.$emit('scan',{codeStr:codeStr})  
        },
		updateQuantity(){
						 
	   }
    }  
}  
</script>  

<style>  
  /*  page {  
        background-color: #efeff4;  
    }  
    .content {  
        text-align: center;  
    }  */
</style>

